<?php 
namespace Api\Controller;
use Think\Controller;
class ApplyController extends BaseController{
	public function dkdj(){
		
		$data['LrCzy']=I('post.user');
		$data['UPzh']=I('post.u_id');
		$data['BeginDate']=I('post.begindate');
		$data['EndDate']=I('post.enddate');
		//$this->ajaxReturn(['code'=>101,'msg'=>'申请成功','data'=>$data]);die;
		$kaishi = (string)$data['BeginDate'];
		$jeishu = (string)$data['EndDate'];
		$data['BeginDate']= date("Y-m-d 00:00:00.000",strtotime($kaishi)); 
		$data['EndDate']= date("Y-m-d 00:00:00.000",strtotime($jeishu)); 
		$pzh=I('post.pzh');
		$data['YsMoney']=I('post.ysmoney');
		$data['ll']=I('post.ll');

		
		$data['ts']=I('post.ts');
		$data['dkYt']=I('post.dkyt');
		$data['jbr']=I('post.jbr');
		//$data['jbdate']=I('post.jbdate');

		$userinfo=M('userinfo')->where(['Pzh'=>$data['UPzh']])->find();
		//print_r($userinfo);die;
		if ($userinfo) {
			$dkinfo=M('dkinfo');
			$row=$dkinfo->field('Pzh')->select();
				foreach ($row as $v) {
					if ($v['Pzh']==$pzh) {
						$pzh=date('ymdHis002',time());
						$pzh='D'.$pzh;
					}
				}
			//创建申请日期
			$lrdate=date('Y-m-d H:i:s',time());
			//添加数据到数据库
			$data['pzh']=$pzh;//订单号
			$data['lrdate']=$lrdate;//申请日期
			$data['jbdate']=I('post.jbdate');
			$data['UName']=$userinfo['uname'];
			$data['ULxdh']=$userinfo['ulxdh'];
			




			$b=$dkinfo->add($data);//添加数据到数据库

			
			if ($b) {
				//写入log日志
					$admin=M('log');
					$row = array(
							
							'czLr' => $data['LrCzy'].'录入了贷款登记，单号：'.$data['Pzh'],
							'CzName' => $data['LrCzy'],
							'czdate' => date('Y-m-d H:i:s',time()),
							'gs'=>'jsapp',
							'yyyy'=>'fox'
						);
					$admin->add($row);


				$this->ajaxReturn(['code'=>101,'msg'=>'申请成功']);

			}else{
				$this->ajaxReturn(['code'=>102,'msg'=>'申请失败']);
			}



		}else{

			$this->ajaxReturn(['code'=>102,'msg'=>'申请失败']);

		}


	

	}

	public function uinfo(){
		$user=I('post.user');
		$userinfo=M('userinfo');
		$login=M('login');
		$qxshow=$login->where(['CzyName'=>$user])->getField('qxshowall');
		// echo $qxshow;die;
		if ($qxshow==1) {
			$data=$userinfo->select();
		}else{
			$data=$userinfo->where(['lrczy'=>$user])->select();
		}
		
		$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
	}

	public function infofind(){
		$id=I('post.id');
		$userinfo=M('userinfo');
		$data=$userinfo->where(['id'=>$id])->find();
	
		
		$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
	}



		public function addinfo(){
			$data['lrczy']=I('post.user_name');
			$pzh=I('post.bianhao');
			$data['ULx']=I('post.ulx');
			$data['UName']=I('post.uname');
			$data['ULxr']=I('post.ulxr');
			$data['ULxdh']=i('post.ulxdh');
			$data['USfz']=I('post.card_num');
			$data['UAdd']=I('post.uadd');
			$data['qs1']=I('post.qs1');
			$data['gx1']=I('post.gx1');
			$data['lxdh1']=I('post.lxdh1');
			$data['dw1']=I('post.dw1');
			$data['jzadd']=I('post.jzadd');
			$data['yuesr']=I('post.yuesr');
			$data['fcqk']=I('post.fcqk');
			$data['clqk']=I('post.clqk');
			$data['sebao']=I('post.sebao');
			$data['gjj']=I('post.gjj');
			$data['lradd']=I('post.lradd');
			$data['uzppath']=I('post.uzppath');	
			if (!$data['ULx']) {
			$this->ajaxReturn(['code'=>103,'msg'=>'申请失败']);die;
			}
			if (!$data['UName']) {
				$this->ajaxReturn(['code'=>104,'msg'=>'申请失败']);die;
			}
			if (!$data['ULxr']) {
				$this->ajaxReturn(['code'=>105,'msg'=>'申请失败']);die;
			}
			if (!$data['ULxdh']) {
				$this->ajaxReturn(['code'=>106,'msg'=>'申请失败']);die;
			}




			$userinfo=M('userinfo');
			$row=$userinfo->field('Pzh')->select();
				foreach ($row as $v) {
					if ($v['Pzh']==$pzh) {
						$pzh=date('ymdHis002',time());
						$pzh='U'.$pzh;
					}
				}

			$lrdate=date('Y-m-d H:i:s',time());
			$data['Pzh']=$pzh;//订单号
			$data['lrdate']=$lrdate;//申请日期
			$b=$userinfo->add($data);//添加数据到数据库
			$login=M('login');
			$data['czyBm']=$login->where(['czyname'=>$data['lrczy']])->getField('czybm');
		
		if ($b) {
			//写入log日志
				$admin=M('log');
				$row = array(
						
						'czLr' => $data['lrczy'].'录入了客户信息，客户单号：'.$data['Pzh'],
						'CzName' => $data['lrczy'],
						'czdate' => date('Y-m-d H:i:s',time()),
						'gs'=>'jsapp',
						'yyyy'=>'fox'
					);
				$admin->add($row);


			$this->ajaxReturn(['code'=>101,'msg'=>'申请成功','data'=>$data['Pzh']]);

		}else{
			$this->ajaxReturn(['code'=>102,'msg'=>'申请失败']);
		}




	}




	public function dksq(){
		
			//获取客户端所有提交过来的信息
		$data['UName']=I('post.apply_unit');//申请人姓名
		$data['ULxr']=I('post.user_name');//联系人姓名
		$data['ULxdh']=I('post.phone');//联系电话
		$data['DkMoney']=I('post.money');//贷款金额
		$data['dkll']=I('post.rate');//贷款利率
		$data['UHj']=I('post.domicile');//户籍
		//$data['UZw']=I('post.posi_tion');//职务
		$data['DkQx']=I('post.loan_time');//贷款期限
		$data['UYGz']=I('post.mouth_salary');//月工资
		$data['USfz']=I('post.card_num');//身份证
		//$data['UCl']=I('post.car');//有无车辆
		$data['UClMx']=I('post.car');//车辆情况
		//$data['UFc']=I('post.house');//有无房产
		$data['UFcMx']=I('post.house');//房产情况
		$data['Uhy']=I('post.marriage');//婚姻状况
		$data['LrCzy']=I('post.user');//录入操作员
		$data['UAdd']=I('post.address');//地址
		//$data['USr']=I('post.sex');//性别

		//创建订单号
		$pzh=date('YmdHis01',time());//订单号
		$dksqinfo=M('dksqinfo');
		$row=$dksqinfo->field('Pzh')->select();
			foreach ($row as $v) {
				if ($v['Pzh']==$pzh) {
					$pzh=date('YmdHis02',time());
				}
			}
		//创建申请日期
		$lrdate=date('Y-m-d H:i:s',time());
		//添加数据到数据库
		$data['Pzh']=$pzh;//订单号
		$data['lrdate']=$lrdate;//申请日期
			//上传图片文件的操作
		    // $config = array(
      //           'maxSize'    =>    3145728,
      //           'rootPath'   =>    './Public/Uploads/', 
      //           'savePath'   =>    '',
      //           'saveName'   =>    array('uniqid',''),
      //           'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
               
      //           'subName'    =>    array('date','Ymd'),
      //       );//上传文件配置
			
           //  $upload = new  \Think\Upload($config);//实例化上传类
           //  if(!file_exists($upload->rootPath)){//根目录不存在，自动创建
           //       mkdir($upload->rootPath);
           //   }
           // $inf   =   $upload->upload();//进行上传
         
           //  if(!$inf){
           //      $this->ajaxReturn(['code'=>102,'msg'=>'上传失败，'.$upload->getError()]);die;
           //  }else{
           //          $arr=array();
           //          foreach($inf as $file){//'http://'.$_SERVER['HTTP_HOST']
           //              $url=$upload->rootPath.$file['savepath'].$file['savename'];
           //              $arr[]=$url;//循环获得图片存放地址
           //          }
           //          $img=implode(",",$arr);
           //          $Model=M('dksqinfo');
           //          $data['USfzZp1']=$img;//申请人身份证图片
                   
              
           //  }
		

			$b=$dksqinfo->add($data);//添加数据到数据库

		
		if ($b) {
			//写入log日志
				$admin=M('log');
				$row = array(
						
						'czLr' => $data['LrCzy'].'录入了贷款申请，单号：'.$data['Pzh'],
						'CzName' => $data['LrCzy'],
						'czdate' => date('Y-m-d H:i:s',time()),
						'gs'=>'jsapp',
						'yyyy'=>'fox'
					);
				$admin->add($row);


			$this->ajaxReturn(['code'=>101,'msg'=>'申请成功']);

		}else{
			$this->ajaxReturn(['code'=>102,'msg'=>'申请失败']);
		}
				


	}

		public function applist(){
		//接收用户名
		$user=I('post.user');
		$param=I('post.param');
		//查询是否显示所有数据还是显示自己
		$login=M('login');
		$hkjhman=M('hkjhman');
		$qxshow=$login->where(['czyname'=>$user])->getField('qxshowall');

		$dkinfo=M('dkinfo');
		$now=date('Y-m-d 00:00:00.000',time());//今天的日期
		if ($param=='check_loan') {
			//根据权限查询数据
			if ($qxshow==1) {
				$data=$dkinfo->where(['shenHe'=>0])->select();
			}else{
				$data=$dkinfo->order('lrdate')->where(['lrczy'=>$user,'shenHe'=>0])->select();
			}

			$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
		}
		if ($param=='newaddloan') {

			//$dkinfo->where(['jbdate'=>$now])->count();//今天新增贷款数量
			if ($qxshow==1) {
				$data=$dkinfo->where(['jbdate'=>$now])->select();
			}else{
				$data=$dkinfo->order('lrdate')->where(['lrczy'=>$user,'jbdate'=>$now])->select();
			}

			$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
		}

		if ($param=='todayloan') {
			//$dkinfo->where(['EndDate'=>$now])->count();//今天到期的贷款数量	
			if ($qxshow==1) {
				$data=$dkinfo->where(['EndDate'=>$now])->select();
			}else{
				$data=$dkinfo->order('lrdate')->where(['lrczy'=>$user,'EndDate'=>$now])->select();
			}

			$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);


		}

		if ($param=='repayment') {
			//$hkjhman->where(['h_Date'=>$now,'h_hkQk'=>'未还款'])->count();//今天需要还款的数量
			if ($qxshow==1) {
				$arr=$hkjhman->where(['h_Date'=>$now,'h_hkQk'=>'未还款'])->select();
			}else{
				$arr=$hkjhman->order('lrdate')->where(['lrczy'=>$user,'h_Date'=>$now,'h_hkQk'=>'未还款'])->select();
			}
			$data=array();
			foreach ($arr as $v) {
				$data=$dkinfo->where(['pzh'=>$v['h_dh']])->select();

			}

			$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);


		}

		if ($param=='overdue') {
			//$DkgtNum=$dkinfo->where(['EndDate'=>['lt',$now]])->count();//今天已经过期的贷款数量
			if ($qxshow==1) {
				$data=$dkinfo->where(['EndDate'=>['lt',$now]])->select();
			}else{
				$data=$dkinfo->order('lrdate')->where(['lrczy'=>$user,'EndDate'=>['lt',$now]])->select();
			}

			$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);


		}



	}

	public function detail(){
		$id=I('post.id');
		$param=I('post.param');
		$dkinfo=M('dkinfo');
		$hkjhman=M('hkjhman');
		switch ($param) {
			case 'check_loan':
				$data=$dkinfo->where(['id'=>$id])->find();
				$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
				break;
			case 'newaddloan':
					$data=$dkinfo->where(['id'=>$id])->find();
				$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
				break;
				case 'todayloan':
					$data=$dkinfo->where(['id'=>$id])->find();
				$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
				break;
				case 'repayment':
					$data=$dkinfo->where(['id'=>$id])->find();
					//$data=$dkinfo->where(['pzh'=>$arr['h_Dh']])->find();
				$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
				break;
				case 'overdue':
					$data=$dkinfo->where(['id'=>$id])->find();
				$this->ajaxReturn(['code'=>101,'msg'=>'查询成功','data'=>$data]);
				break;
		
		}

		

	}



	public function loan(){
		$text=I('post.data');
		$type=I('post.type');
		$id=I('post.id');
		$user=I('post.user');
		$login=M('login');
		$dkinfo=M('dkinfo');//贷款表实例化
		$login=$login->where(['czyname'=>$user])->find();//

		if ($type==1) {
				if ($login['qxshenhe']==1) {
				$sh=$dkinfo->where(['id'=>$id])->find();
				if ($sh['shenhe']==1) {
					$this->ajaxReturn(['code'=>102,'msg'=>'已经审核过了']);die;
				}else{
					$data['shenHe']=1;
					$data['shenheYj']=$text;
					$data['ShenHeCzy']=$user;
					$data['shenheLx']='自动审核';
					$data['shenheDate']=date('Y-m-d 00:00:00.000');
					$b=$dkinfo->where(['id'=>$id])->save($data);
					if ($b) {
						$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);die;
					}else{
						$this->ajaxReturn(['code'=>101,'msg'=>'操作失败']);die;
					}
				}



			}else{
				$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);
			}



		}else{

			if ($login['qxshenhe']==1) {
				$sh=$dkinfo->where(['id'=>$id])->find();
				if ($sh['shenhe']==2) {
					$this->ajaxReturn(['code'=>102,'msg'=>'已经拒绝过了']);die;
				}else{
					$data['shenHe']=2;
					$data['shenheYj']=$text;
					$data['ShenHeCzy']=$user;
					$data['shenheLx']='自动审核';
					$data['shenheDate']=date('Y-m-d 00:00:00.000');
					$b=$dkinfo->where(['id'=>$id])->save($data);
					if ($b) {
						$this->ajaxReturn(['code'=>101,'msg'=>'操作成功']);die;
					}else{
						$this->ajaxReturn(['code'=>101,'msg'=>'操作失败']);die;
					}
				}



			}else{
				$this->ajaxReturn(['code'=>102,'msg'=>'没有权限操作']);
			}




		}

		


	}





}













 
?>

















